package a.student.dao;

import java.util.ArrayList;

import a.student.library.*;
import a.student.helper.DatabaseHelper;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class AssigntmentDAO extends BaseDao<Assignment>{

	public AssigntmentDAO(Context context, String databaseName) {
		super(context, databaseName);
	}
	
	public AssigntmentDAO(SQLiteDatabase db) {
		super(db);
	}
	
	public AssigntmentDAO(DatabaseHelper helper) {
		super(helper);
	}
	
	public long addNewAssignment(Assignment assignmentInfo){
		String done = assignmentInfo.done?"1":"0";
		
		ContentValues cv = new ContentValues();
		cv.put(Assignment.ASSIGNMENT_NAME, assignmentInfo.name);
		cv.put(Assignment.ASSIGNMENT_PRIORITY, assignmentInfo.priority);
		cv.put(Assignment.ASSIGNMENT_DUEDATE, assignmentInfo.dueDate);
		cv.put(Assignment.ASSIGNMENT_DESC, assignmentInfo.des);
		cv.put(Assignment.ASSIGNMENT_ALERTTIME, assignmentInfo.alertTime);
		cv.put(Assignment.ASSIGNMENT_DONE, done);
		cv.put(Assignment.ASSIGNMENT_CLID, assignmentInfo.classID);
		
		long result = super.insert(Assignment.ASSIGNMENT_TBL_NAME, cv);
		if(result > 0)
			assignmentInfo.setAssignmentId((int)result);
		return result;
		
	}

	public long removeAssignment(Assignment assignmentInfo){
		
		ContentValues cv = new ContentValues();
		cv.put(Assignment.ASSIGNMENT_ID, assignmentInfo.getAssignmentId());
		long count = super.delete(Assignment.ASSIGNMENT_TBL_NAME, cv);
		return count;
		
	}
    
    public ArrayList<Assignment> getAllAssignment(){
    	Cursor c = super.getAll(Assignment.ASSIGNMENT_TBL_NAME, Assignment.ASSIGNMENT_DEFAULT_SORT_ORDER);
    	ArrayList<Assignment> array = new ArrayList<Assignment>();
    	if(c==null)
    		return array;
    	while(!c.isAfterLast()){
    		array.add(new Assignment(c));
    		c.moveToNext();
    	}
    	c.close();
    	return array;
    }
    
    public Assignment findAssignment(final int assignmentID) {
        ContentValues cv = new ContentValues();
        cv.put(Assignment.ASSIGNMENT_ID, assignmentID);
        Cursor c = super.find(Assignment.ASSIGNMENT_TBL_NAME, cv,Assignment.ASSIGNMENT_DEFAULT_SORT_ORDER);
        Assignment assigment = null;
        if (c != null) {
            try {
            	assigment = new Assignment(c);
            } catch (Exception e) {
            	e.printStackTrace();
			}
            finally {
                c.close();
            }
        }
        return assigment;
    }
    
	public boolean updateAssignmentDetail(Assignment assignmentInfo){
		boolean flag = true;
		ContentValues cv = new ContentValues();
		cv.put(Assignment.ASSIGNMENT_NAME, assignmentInfo.name);
		cv.put(Assignment.ASSIGNMENT_PRIORITY, assignmentInfo.priority);
		cv.put(Assignment.ASSIGNMENT_DUEDATE, assignmentInfo.dueDate);
		cv.put(Assignment.ASSIGNMENT_DESC, assignmentInfo.des);
		cv.put(Assignment.ASSIGNMENT_ALERTTIME, assignmentInfo.alertTime);
		cv.put(Assignment.ASSIGNMENT_DONE, assignmentInfo.done);
		flag = super.update(Assignment.ASSIGNMENT_TBL_NAME, cv, Assignment.ASSIGNMENT_ID + "=?", new String[]{String.valueOf(assignmentInfo.getAssignmentId())})>0;
    	return flag;
	}


}
